<?php
class Admin_AuthController extends Zend_Controller_Action
{
	public function indexAction()
	{
		$this->myRedirect();
	}
	private function myRedirect(){
           $this->_redirect("/admin");
	}
	public function loginAction()
	{
		$this->_helper->layout()->disablelayout();
		if (Zend_Auth::getInstance()->hasIdentity())
		{
			$this->myRedirect();
		}
		$request = $this->_request;
		$captcha = new Zend_Captcha_Image();
		$captcha->setTimeout('300')
				->setWordLen('4')
				->setHeight('80')
				->setWidth('160')
				->setFontSize(25)
				->setImgDir(BASE_PATH . 'upload/captcha/')
				->setImgUrl(BASE_URL.'upload/captcha/')
				->setFont(BASE_PATH.'upload/captcha/font/captcha.ttf');
		$captcha->generate();
		$this->view->captcha = $captcha->render();
		$this->view->captchaID = $captcha->getId();
		// Dua chuoi Captcha vao session
		$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captcha->getId());
		$captchaSession->word = $captcha->getWord();
		if ($this->_request->isPost())
		{		
			// Captcha -----
			$captchaID = $this->_request->captcha_id;
			
	 		$captchaSession = new Zend_Session_Namespace('Zend_Form_Captcha_' . $captchaID);
	 		$captchaIterator = $captchaSession->getIterator();
	 		$captchaWord = $captchaIterator['word'];
	 		
	 		if(strtolower($this->_request->captcha) != strtolower($captchaWord)){
	 			$message = array("Captcha not match !");
				$this->view->message = $message;
	 			return;
	 		}
	 		//------------------------------
			$authAdapter = $this->getAuthAdapter();
			$user = trim($request->getParam("username"));
			$pass = H_Cryptography_Useracess::encoding(trim($request->getParam("password")));
			$authAdapter->setIdentity($user);
			$authAdapter->setCredential($pass);
			$auth = Zend_Auth::getInstance();
			$result = $auth->authenticate($authAdapter);
			if ($result->isValid())
			{
				$identity = $authAdapter->getResultRowObject();
				$authStorage = $auth->getStorage();
				$authStorage->write($identity);
				$this->myRedirect();
			}
			else
			{
				$message = array("Username or password is not correct!");
				$this->view->message = $message;
			}
		}		
	}
	
	//Called by ajax
	public function changepassAction(){
		$this->_helper->layout()->disablelayout();
		$this->_helper->viewRenderer->setNoRender(true);
		if (!Zend_Auth::getInstance()->hasIdentity())
		{
			echo "Not authentication";
			return;
		}
		$authStorage = Zend_Auth::getInstance()->getStorage();
		$userdata = $authStorage->read();
		$oUser = new H_Model_User();
		$data = $this->_request->getParams();
		
		$response = array();
		
                if($oUser->updateUser($userdata->id, $data)){
                        $response['sucess'] = true;
                        $response['message'][] = "Update Secuess!";
                }else{
                        $response['sucess'] = false;
                        $response['message'][] = "Update Fail!";
                }
		
		echo Zend_Json::encode($response);
	}
	
	public function logoutAction()
	{
		$session = new Zend_Session_Namespace('currentModule');
		$session->unsetAll();    	
		Zend_Auth::getInstance()->clearIdentity();
		$this->_redirect(BASE_URL."admin/auth/login");
	}
	private function getAuthAdapter()
	{
		$user = new H_Model_User();
		$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
		$authAdapter->setTableName($user->info(H_Model_User::NAME))
					->setIdentityColumn("username")
					->setCredentialColumn("password");
		return $authAdapter;			
	}
}

